/*
 * EditarFamilia.java
 *
 * Created on 25/02/2011, 12:20:48
 */
package pbf.dialog;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import org.hibernate.Session;
import pbf.conf.hibernate.HibernateUtil;
import pbf.conf.hibernate.entidades.Familia;
import pbf.conf.hibernate.entidades.Localidade;
import pbf.principal.SistemaDeAgendamento;
import pbf.util.BuscaIndexFamiliaLocalidade;
import pbf.util.DataHora;
import pbf.util.Mensagem;
import pbf.util.ValidaCpf;
import pbf.util.ValidaString;

/**
 *
 * @author Ricardo
 */
public class EditarFamilia extends javax.swing.JDialog {

    private Familia familia = null;
    private ArrayList<Localidade> listaLocalidade;
    private Session sessao = null;

    public EditarFamilia(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        sessao = HibernateUtil.openSession();
        setLocalidade();
    }

    private void setLocalidade() {
        abrirSessao();
        localidadeNome.removeAllItems();
        listaLocalidade = (ArrayList<Localidade>) sessao.createQuery("From pbf.conf.hibernate.entidades.Localidade order by LOCALIDADE_ID").list();

        for (Localidade l : listaLocalidade) {
            localidadeNome.addItem(l.getLocalidadeNome());
        }
        fecharSessao();
    }

    public void setFormulario(Familia f) {
        this.familia = f;
        familiaNome.setText(familia.getFamiliaNome());
        familiaCpf.setText(familia.getFamiliaCpf());
        familiaTitulo.setText(familia.getFamiliaTitulo());
        familiaIdentidade.setText(familia.getFamiliaIdentidade());
        familiaTelefone.setText(familia.getFamiliaTelefone());
        familiaCelular.setText(familia.getFamiliaCelular());
        familiaNis.setText("" + familia.getFamiliaNis());
        familiaEndereco.setText(familia.getFamiliaEndereco());
        familiaNumPessoas.setText("" + familia.getFamiliaNPessoas());
        familiaComplemento.setText(familia.getFamiliaComplementoEndereco());

        if (familia.getFamiliaBeneficiario().equals("S")) {
            familiaBeneficiario.setSelectedIndex(0);
        } else {
            familiaBeneficiario.setSelectedIndex(1);
        }

        if (familia.getFamiliaSexo().equals("M")) {
            familiaSexo.setSelectedIndex(1);
        } else {
            familiaSexo.setSelectedIndex(2);
        }
        localidadeNome.setSelectedIndex(BuscaIndexFamiliaLocalidade.getIndex(listaLocalidade, familia.getLocalidade().getLocalidadeId()));
    }

    private void abrirSessao() {
        sessao.beginTransaction();
    }

    private void fecharSessao(){
        sessao.getTransaction().commit();
    }

    private boolean isFormularioValido() {
        if (familiaNome.getText().equals("")) {
            Mensagem.mensageiroAtencao("Preencha o campo Nome do Responsável");
            return false;
        }

        if (familiaSexo.getSelectedIndex() == 0) {
            Mensagem.mensageiroAtencao("Preencha o campo Sexo");
            return false;
        }

        if (familiaEndereco.getText().equals("")) {
            Mensagem.mensageiroAtencao("Preencha o campo Endereço");
            return false;
        }

        if (familiaNumPessoas.getText().equals("")) {
            Mensagem.mensageiroAtencao("Preencha o campo Número de Pessoas");
            return false;
        }

        if (!familiaCpf.getText().equals("   .   .   -  ")) {
            if (!ValidaCpf.validaCpf(familiaCpf.getText())) {
                Mensagem.mensageiroAtencao("CPF inválido");
                return false;
            }
        }
        return true;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        familiaNome = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        familiaBeneficiario = new javax.swing.JComboBox();
        jLabel12 = new javax.swing.JLabel();
        familiaIdentidade = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        familiaTitulo = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        familiaCelular = new javax.swing.JFormattedTextField();
        familiaTelefone = new javax.swing.JFormattedTextField();
        jLabel18 = new javax.swing.JLabel();
        familiaCpf = new javax.swing.JFormattedTextField();
        familiaSexo = new javax.swing.JComboBox();
        jLabel19 = new javax.swing.JLabel();
        familiaNis = new javax.swing.JFormattedTextField();
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel4 = new javax.swing.JLabel();
        familiaEndereco = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        familiaNumPessoas = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        localidadeNome = new javax.swing.JComboBox();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        familiaComplemento = new javax.swing.JTextArea();
        jButton7 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel1.setText("Nome do Responsável");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(10, 10, 480, 20);

        familiaNome.setFont(new java.awt.Font("Tahoma", 0, 14));
        familiaNome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                familiaNomeKeyTyped(evt);
            }
        });
        jPanel1.add(familiaNome);
        familiaNome.setBounds(10, 40, 480, 23);

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel2.setText("CPF");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(10, 70, 130, 20);

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel3.setText("Beneficiário");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(350, 70, 120, 20);

        familiaBeneficiario.setFont(new java.awt.Font("Tahoma", 0, 14));
        familiaBeneficiario.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Sim", "Não" }));
        jPanel1.add(familiaBeneficiario);
        familiaBeneficiario.setBounds(350, 90, 140, 23);

        jLabel12.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel12.setText("Identidade");
        jPanel1.add(jLabel12);
        jLabel12.setBounds(10, 120, 130, 20);

        familiaIdentidade.setFont(new java.awt.Font("Tahoma", 0, 14));
        jPanel1.add(familiaIdentidade);
        familiaIdentidade.setBounds(10, 140, 160, 23);

        jLabel13.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel13.setText("Título");
        jPanel1.add(jLabel13);
        jLabel13.setBounds(180, 70, 180, 20);

        familiaTitulo.setFont(new java.awt.Font("Tahoma", 0, 14));
        jPanel1.add(familiaTitulo);
        familiaTitulo.setBounds(180, 90, 160, 23);

        jLabel15.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel15.setText("Telefone");
        jPanel1.add(jLabel15);
        jLabel15.setBounds(180, 120, 130, 20);

        jLabel16.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel16.setText("Celular");
        jPanel1.add(jLabel16);
        jLabel16.setBounds(350, 120, 130, 20);

        try {
            familiaCelular.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(63) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        familiaCelular.setFont(new java.awt.Font("Tahoma", 0, 14));
        jPanel1.add(familiaCelular);
        familiaCelular.setBounds(350, 140, 140, 23);

        try {
            familiaTelefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(63) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        familiaTelefone.setFont(new java.awt.Font("Tahoma", 0, 14));
        jPanel1.add(familiaTelefone);
        familiaTelefone.setBounds(180, 140, 160, 23);

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel18.setText("NIS");
        jPanel1.add(jLabel18);
        jLabel18.setBounds(10, 170, 130, 20);

        try {
            familiaCpf.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        familiaCpf.setFont(new java.awt.Font("Tahoma", 0, 14));
        jPanel1.add(familiaCpf);
        familiaCpf.setBounds(10, 90, 160, 23);

        familiaSexo.setFont(new java.awt.Font("Tahoma", 0, 14));
        familiaSexo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione", "Masculino", "Feminino" }));
        jPanel1.add(familiaSexo);
        familiaSexo.setBounds(180, 190, 160, 23);

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel19.setText("Sexo");
        jPanel1.add(jLabel19);
        jLabel19.setBounds(180, 170, 120, 20);

        try {
            familiaNis.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("####.####.###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        familiaNis.setFont(new java.awt.Font("Tahoma", 0, 14));
        jPanel1.add(familiaNis);
        familiaNis.setBounds(10, 190, 160, 23);

        jTabbedPane1.addTab("Dados do Responsável Família", jPanel1);

        jPanel2.setLayout(null);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/arrow_refresh.png"))); // NOI18N
        jButton1.setText("Atualizar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton1);
        jButton1.setBounds(380, 250, 110, 25);
        jPanel2.add(jSeparator1);
        jSeparator1.setBounds(0, 240, 520, 10);

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel4.setText("Endereço:");
        jPanel2.add(jLabel4);
        jLabel4.setBounds(20, 20, 90, 20);

        familiaEndereco.setFont(new java.awt.Font("Tahoma", 0, 14));
        jPanel2.add(familiaEndereco);
        familiaEndereco.setBounds(110, 20, 380, 23);

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel5.setText("Número de Pessoas da Família:");
        jPanel2.add(jLabel5);
        jLabel5.setBounds(20, 80, 240, 20);

        familiaNumPessoas.setFont(new java.awt.Font("Tahoma", 0, 14));
        jPanel2.add(familiaNumPessoas);
        familiaNumPessoas.setBounds(260, 80, 230, 23);

        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel6.setText("Localidade:");
        jPanel2.add(jLabel6);
        jLabel6.setBounds(20, 50, 90, 20);

        localidadeNome.setFont(new java.awt.Font("Tahoma", 0, 14));
        localidadeNome.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Selecione", "Centro da Cidade", "Vila São Miguel", "Vila São José", "Vila Quixaba", "Lagoa do Romão", "Novo Nilo", "Tucum", "Entroncamento do Jaú" }));
        jPanel2.add(localidadeNome);
        localidadeNome.setBounds(110, 50, 260, 23);

        jLabel11.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel11.setText("Complemento:");
        jPanel2.add(jLabel11);
        jLabel11.setBounds(20, 110, 470, 20);

        familiaComplemento.setColumns(20);
        familiaComplemento.setFont(new java.awt.Font("Arial", 0, 16));
        familiaComplemento.setRows(5);
        jScrollPane2.setViewportView(familiaComplemento);

        jPanel2.add(jScrollPane2);
        jScrollPane2.setBounds(20, 130, 470, 80);

        jButton7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/toggle-expand.png"))); // NOI18N
        jButton7.setText("Outros");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton7);
        jButton7.setBounds(380, 50, 110, 25);

        jTabbedPane1.addTab("Dados de Endereço da Família", jPanel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 520, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 319, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(49, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void familiaNomeKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_familiaNomeKeyTyped
        ValidaString.protecaoContraAspas(evt);
}//GEN-LAST:event_familiaNomeKeyTyped

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
        CadastrarLocalidade cadLocalidade = new CadastrarLocalidade(null, true);
        cadLocalidade.setVisible(true);
        setLocalidade();
}//GEN-LAST:event_jButton7ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if(isFormularioValido()){
        familia.setFamiliaNome(familiaNome.getText());
        familia.setFamiliaCpf(familiaCpf.getText());
        familia.setFamiliaTitulo(familiaTitulo.getText());
        familia.setFamiliaIdentidade(familiaIdentidade.getText());
        familia.setFamiliaTelefone(familiaTelefone.getText());
        familia.setFamiliaCelular(familiaCelular.getText());
        familia.setFamiliaNis(familiaNis.getText());
        familia.setFamiliaEndereco(familiaEndereco.getText());
        familia.setFamiliaNPessoas(Integer.parseInt(familiaNumPessoas.getText()));
        familia.setFamiliaComplementoEndereco(familiaComplemento.getText());
        familia.setFamiliaAtivo("S");
        familia.setUsuario(SistemaDeAgendamento.getUsuario());
        Localidade l = listaLocalidade.get(localidadeNome.getSelectedIndex());
        familia.setLocalidade(l);
        familia.setFamiliaDtAlteracao(DataHora.getCalendar().getTime());

        if (familiaBeneficiario.getSelectedIndex() == 0) {
            familia.setFamiliaBeneficiario("S");
        }

        if (familiaBeneficiario.getSelectedIndex() == 1) {
            familia.setFamiliaBeneficiario("N");
        }

        if (familiaSexo.getSelectedIndex() == 1) {
            familia.setFamiliaSexo("M");
        }

        if (familiaSexo.getSelectedIndex() == 2) {
            familia.setFamiliaSexo("F");
        }

        abrirSessao();
        sessao.update(familia);
        JOptionPane.showMessageDialog(null, "Familia Alterada com sucesso!");
        dispose();
        fecharSessao();
        }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
       sessao.close();
    }//GEN-LAST:event_formWindowClosing

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                EditarFamilia dialog = new EditarFamilia(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox familiaBeneficiario;
    private javax.swing.JFormattedTextField familiaCelular;
    private javax.swing.JTextArea familiaComplemento;
    private javax.swing.JFormattedTextField familiaCpf;
    private javax.swing.JTextField familiaEndereco;
    private javax.swing.JTextField familiaIdentidade;
    private javax.swing.JFormattedTextField familiaNis;
    private javax.swing.JTextField familiaNome;
    private javax.swing.JTextField familiaNumPessoas;
    private javax.swing.JComboBox familiaSexo;
    private javax.swing.JFormattedTextField familiaTelefone;
    private javax.swing.JTextField familiaTitulo;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JComboBox localidadeNome;
    // End of variables declaration//GEN-END:variables
}
